package br.com.stefanini.provajava.controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.stefanini.provajava.model.Experiencia;
import br.com.stefanini.provajava.model.Profissional;
import br.com.stefanini.provajava.model.service.ProfissionalService;

@WebServlet("/ProfissionalController")
public class ProfissionalController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static String INSERT_OR_EDIT = "/pages/profissional.jsp";
	private static String LIST = "/pages/listProfissional.jsp";
	private ProfissionalService service;

	public ProfissionalController() {
		super();
		service = new ProfissionalService();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String forward = "";
		String action = request.getParameter("action");

		if (action.equalsIgnoreCase("delete")) {
			service.deleteProfissional(Integer.parseInt(request
					.getParameter("idProfissional")));
			forward = LIST;
			request.setAttribute("listProfissional",
					service.getAllProfissionais());
		} else if (action.equalsIgnoreCase("edit")) {
			forward = INSERT_OR_EDIT;
			request.setAttribute("profissional", service
					.getProfissionalById(Integer.parseInt(request
							.getParameter("idProfissional"))));
		} else if (action.equalsIgnoreCase("list")) {
			forward = LIST;
			request.setAttribute("listProfissional",
					service.getAllProfissionais());
		} else if (action.equalsIgnoreCase("deleteExp")) {
			service.deleteExperiencia(Integer.parseInt(request.getParameter("idExperiencia")));
			forward = INSERT_OR_EDIT;
			request.setAttribute("profissional", service.getProfissionalById(Integer.parseInt(request.getParameter("idProfissional"))));
		} else {
			forward = INSERT_OR_EDIT;
		}

		RequestDispatcher view = request.getRequestDispatcher(forward);
		view.forward(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		Profissional profissional = new Profissional();
		profissional.setNome(request.getParameter("nome"));
		profissional.setEmail(request.getParameter("email"));
		try {
			profissional.setNascimento(new SimpleDateFormat("dd/MM/yyyy")
					.parse(request.getParameter("nascimento")));
		} catch (ParseException e) {
			e.printStackTrace();
		}

		String idProfissional = request.getParameter("idProfissional");
		if (idProfissional == null || idProfissional.isEmpty()) {
			service.addProfissional(profissional);
		} else {
			profissional.setId(Integer.parseInt(idProfissional));
			service.updateProfissional(profissional);
		}
		RequestDispatcher view = request.getRequestDispatcher(LIST);
		request.setAttribute("listProfissional", service.getAllProfissionais());
		view.forward(request, response);
	}
}
